import requests
import datetime
import logging
from django.conf import settings

logger = logging.getLogger('VideoHandler')

def send_to_person_in_group(user_id, msg, detail):
    url = settings.FEISHU_NOTIFY['send_to_group_url']
    if user_id is None:
        user_id = 888
    body = {
        "sourceTag": "video-handler-center",
        "outline": msg,
        "stackTrace": detail,
        "user": user_id,
        "time": int(datetime.datetime.now().timestamp() * 1000)
    }
    logger.debug(f"发送飞书消息体: {body}")
    try:
        # 发送 POST 请求
        response = requests.post(url, json=body)
        # 检查响应状态码
        if response.status_code == 200:
            # 打印响应内容
            logger.info(f'发送飞书消息成功: {response.json()}')
        else:
            logger.warning(f'发送飞书消息失败: {response}')
    except requests.RequestException as e:
        logger.error(f'发送飞书消息发生错误: {e}', exc_info=True)